##				  ##
# base file to run #
##				  ##


import os
import sys
import time

monitors=[]
interfaces=[]
sys.path.append("interfaces/")
sys.path.append("monitors/")
	
def readConfig():
	try:
		conf= open("settings.conf",'r')
	except:
		return None
	lines = conf.readlines()
	config = {}
	for line in lines:
		var, value = line.split('=')
		var = var.strip()
		value = value.strip()
		config[var] = value
	return config
def loadMonitors():
    global monitors
    modulenames=[]
    mlist=os.listdir("monitors/")
    for m in mlist:
        if(m.find(".pyc")==-1): #fugly code
            if(m.find(".py")>-1):
                modulenames.append(m[0:m.find(".py")])
    monitors=map(__import__, modulenames)
    modulenames.reverse()
    modulesinit=[]
    for mon in monitors:
        mon=getattr(mon,modulenames.pop())
        modulesinit.append(mon())
    monitors=modulesinit
def loadInterfaces():
    global interfaces
    interfacenames=[]
    ilist=os.listdir("interfaces/")
    for i in ilist:
        if(i.find(".pyc")==-1): #fugly code 
            if(i.find(".py")>-1):
                name=i[0:i.find(".py")]
                interfacenames.append(name)
    interfaces=map(__import__, interfacenames)
    interfacenames.reverse()
    interfacesinit=[]
    for int in interfaces:
        int=getattr(int,interfacenames.pop())
        interfacesinit.append(int())
    interfaces=interfacesinit
    
config=readConfig()
loadMonitors()
print "Monitors loaded!\n"
loadInterfaces()
print "Interfaces loaded!\n"


#exec the login routines

for interface in interfaces:
    print interface.info() #just a test to show the info functions
    interface.login(config['username'],config['password'])

#infinite loop
    
while 1:
    for interface in interfaces:
        interface.loop()
    for m in monitors:
        #print "Running "+m.name+"'s check routine...\n"
        if(m.check()==1):
        	for interface in interfaces:
        		interface.alert(m.name,m.type,m.result)
    time.sleep(0.1)

